﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eFirmware : EditBase
    {

        public string FilePath
        {
            get { return Server.MapPath("../firmware"); }
        }

        #region Public Members

        public bool IsNewItem
        {
            get { return (ActiveItemId == Guid.Empty); }
        }

        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        public Guid ModelId
        {
            get { return (hfModelId.Value != string.Empty) ? new Guid(hfModelId.Value) : Guid.Empty; }
            set { hfModelId.Value = value.ToString(); }
        }


        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {

            ibUpdateFiles.Click += new ImageClickEventHandler(ibUpdateFiles_Click);


            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            if (ParentId != Guid.Empty)
                ModelId = ParentId;                

            if (ActiveItemId != Guid.Empty)
            {
                bindItem();
            }
            else
            {
                bindLists(new FirmwareInfo());
            }

            string script = "function onClientClose(oWnd,args) { var arg = args.get_argument(); if(arg){document.getElementById('" + hfFileUploaded.ClientID + "').value = arg.fileName; document.getElementById('" + ibUpdateFiles.ClientID + "').click();}}";
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "test", script, true);
        }

        protected void ibUpdateFiles_Click(object sender, ImageClickEventArgs e)
        {
            bindFirmWareFiles();
            if (ddFiles.FindItemByValue(hfFileUploaded.Value) != null)
                ddFiles.FindItemByValue(hfFileUploaded.Value).Selected = true;
        }



        #endregion


        #region Helpers

        private void bindItem()
        {
            FirmwareController fc = new FirmwareController();
            FirmwareInfo fi = fc.SelectFirmware(ActiveItemId);

            if (fi != null)
            {
                lblHeaderLeft.Text = "Firmware - " + fi.Version;
                
                txtVersion.Text = fi.Version;
                txtDescription.Text = fi.Description;
                txtBootLoader.Text = fi.BootLoader;
                chkSendXml.Checked = fi.SendXML;
                ModelId = fi.ModelId;
                bindLists(fi);
            }


        }

        private void bindLists(FirmwareInfo fi)
        {
            if (ModelId!=Guid.Empty)
            {
                ModelController mc = new ModelController();
                ModelInfo mi = mc.SelectModel(ModelId);
                if (mi != null)
                {
                    lblModel.Text = mi.Name;
                    lblHeaderRight.Text = mi.Name;
                }
            }
            
            
            // Configurations
            ConfigurationController cc = new ConfigurationController();
            List<ConfigurationInfo> ciList = cc.SelectConfigurations("Name Asc");
            ddConfiguration.DataSource = ciList;
            ddConfiguration.DataTextField = "Name";
            ddConfiguration.DataValueField = "ConfigurationId";
            ddConfiguration.DataBind();
            ddConfiguration.Items.Insert(0, new RadComboBoxItem("<None>", Guid.Empty.ToString()));
            if (fi.ConfigurationId != Guid.Empty & ddConfiguration.Items.FindItemByValue(fi.ConfigurationId.ToString()) != null)
                ddConfiguration.Items.FindItemByValue(fi.ConfigurationId.ToString()).Selected = true;


            // Firmware UpdateTo
            FirmwareController fc = new FirmwareController();
            List<FirmwareInfo> fiList = fc.SelectFirmwaresByModel(ModelId, "Version ASC");
            
            FirmwareInfo iRemove = new FirmwareInfo(); ;
            foreach (FirmwareInfo fiItem in fiList)
            {
                if (fiItem.FirmwareId == fi.FirmwareId)
                    iRemove = fiItem;
            }
            if (iRemove.FirmwareId != Guid.Empty)
                fiList.Remove(iRemove);

            ddFirmware.DataSource = fiList;
            ddFirmware.DataTextField = "Version";
            ddFirmware.DataValueField = "FirmwareId";
            ddFirmware.DataBind();
            ddFirmware.Items.Insert(0, new RadComboBoxItem("<None>", Guid.Empty.ToString()));


            if (fi.UpgardeTo != Guid.Empty & ddFirmware.Items.FindItemByValue(fi.UpgardeTo.ToString()) != null)
                ddFirmware.Items.FindItemByValue(fi.UpgardeTo.ToString()).Selected = true;

            bindFirmWareFiles();
            if (fi.FileUrl != string.Empty & ddFiles.Items.FindItemByValue(fi.FileUrl) != null)
                ddFiles.Items.FindItemByValue(fi.FileUrl.ToString()).Selected = true;

        }


        public void bindFirmWareFiles()
        {
            // Bin files
            ddFiles.DataSource = GetFirmwareFiles();
            ddFiles.DataTextField = "Name";
            ddFiles.DataValueField = "Name";
            ddFiles.DataBind();
            ddFiles.Items.Insert(0, new RadComboBoxItem("<None>", ""));
        }


        public FileInfo[] GetFirmwareFiles()
        {
            DirectoryInfo di = new DirectoryInfo(FilePath);
            return di.GetFiles();
        }

        #endregion


        #region Action Event Helpers


        private void SaveItem()
        {
            if (Page.IsValid)
            {
                FirmwareController fc = new FirmwareController();
                FirmwareInfo fi = new FirmwareInfo();

                if (!IsNewItem)
                    fi = fc.SelectFirmware(ActiveItemId);

                fi.Version = txtVersion.Text;
                fi.Description = txtDescription.Text;
                fi.BootLoader = txtBootLoader.Text;
                fi.SendXML = chkSendXml.Checked;
                fi.ModelId = ModelId;

                if (ddConfiguration.SelectedIndex > 0)
                    fi.ConfigurationId = new Guid(ddConfiguration.SelectedValue);
                else
                    fi.ConfigurationId = new Guid();


                if (ddFirmware.SelectedIndex > 0)
                    fi.UpgardeTo = new Guid(ddFirmware.SelectedValue);
                else
                    fi.UpgardeTo = new Guid();

                if (ddFiles.SelectedValue!= "")
                    fi.FileUrl = ddFiles.SelectedValue;
                else
                    fi.FileUrl = "";


                if (IsNewItem)
                    fc.InsertFirmware(fi);
                else
                    fc.UpdateFirmware(fi);


                RaiseNavEvent(this, new NavEventArgs("eModel", ModelId));
            }


        }


        private void DeleteItem()
        {
            FirmwareController fc = new FirmwareController();
            fc.DeleteFirmware(ActiveItemId);

            RaiseNavEvent(this, new NavEventArgs("eModel", ModelId));
        }


        private void ReturnToModel()
        {
            FirmwareController fc = new FirmwareController();
            FirmwareInfo fi = fc.SelectFirmware(ActiveItemId);
            RaiseNavEvent(this, new NavEventArgs("eModel", ModelId));
        }


        #endregion


        #region EditBase
        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Save":
                    SaveItem();
                    break;
                case "Delete":
                    DeleteItem();
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eModel", ModelId));
                    break;
                default:
                    break;
            }

        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Save", "Save Changes", "~/images/icons/save.png", "", true, true));
                al.Add(new ActionInfo("Delete", "Delete Firmware", "~/images/icons/delete.png", "Are you sure you want to delete this firmware", !IsNewItem));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));
                return al;

            }
        }

        #endregion
    }
}